package com.ruoyi.recruit.controller;

import java.util.List;

import com.ruoyi.api.system.utils.ShiroUtils;
import com.ruoyi.api.system.utils.poi.ExcelUtil;
import com.ruoyi.common.utils.StringUtils;
import com.ruoyi.recruit.constant.RecruitConstants;
import com.ruoyi.recruit.domain.RecruitContent;
import com.ruoyi.recruit.domain.RecruitInfo;
import com.ruoyi.recruit.service.*;
import org.apache.shiro.authz.annotation.RequiresPermissions;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Controller;
import org.springframework.ui.ModelMap;
import org.springframework.web.bind.annotation.GetMapping;
import org.springframework.web.bind.annotation.PathVariable;
import org.springframework.web.bind.annotation.PostMapping;
import org.springframework.web.bind.annotation.RequestMapping;
import org.springframework.web.bind.annotation.ResponseBody;
import com.ruoyi.common.annotation.Log;
import com.ruoyi.common.enums.BusinessType;
import com.ruoyi.common.core.controller.BaseController;
import com.ruoyi.common.core.domain.AjaxResult;
import com.ruoyi.common.core.page.TableDataInfo;
import org.springframework.web.multipart.MultipartFile;

/**
 * 招聘内容Controller
 * 
 * @author ruoyi
 * @date 2022-05-03
 */
@Controller
@RequestMapping("/recruit/content")
public class RecruitContentController extends BaseController
{
    private String prefix = "recruit/content";

    @Autowired
    private IRecruitContentService recruitContentService;

    @Autowired
    private IRecruitInfoService recruitInfoService;

    @Autowired
    private IRecruitUserService recruitUserService;

    @Autowired
    private IRecruitUserWrittenService recruitUserWrittenService;

    @Autowired
    private IRecruitUserInterviewService recruitUserInterviewService;

    @Autowired
    private IRecruitUserConfirmService recruitUserConfirmService;

    @RequiresPermissions("recruit:content:view")
    @GetMapping(value = {"", "/{recruitId}"})
    public String content(@PathVariable(value = "recruitId", required = false) Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(StringUtils.isNotNull(recruitId)){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/content";
    }

    /**
     * 查询招聘内容列表
     */
    @RequiresPermissions("recruit:content:list")
    @PostMapping("/list")
    @ResponseBody
    public TableDataInfo list(RecruitContent recruitContent)
    {
        startPage();
        List<RecruitContent> list = recruitContentService.selectRecruitContentList(recruitContent);
        return getDataTable(list);
    }

    /**
     * 导出招聘内容列表
     */
    @RequiresPermissions("recruit:content:export")
    @Log(title = "招聘内容", businessType = BusinessType.EXPORT)
    @PostMapping("/export")
    @ResponseBody
    public AjaxResult export(RecruitContent recruitContent)
    {
        List<RecruitContent> list = recruitContentService.selectRecruitContentList(recruitContent);
        ExcelUtil<RecruitContent> util = new ExcelUtil<RecruitContent>(RecruitContent.class);
        return util.exportExcel(list, "招聘内容数据");
    }

    /**
     * 导入招聘计划
     * @param file
     * @param recruitId
     * @param updateSupport
     * @return
     * @throws Exception
     */
    @Log(title = "招聘计划", businessType = BusinessType.IMPORT)
    @RequiresPermissions("recruit:content:import")
    @PostMapping("/importData")
    @ResponseBody
    public AjaxResult importData(MultipartFile file, Long recruitId, boolean updateSupport) throws Exception
    {
        ExcelUtil<RecruitContent> util = new ExcelUtil<RecruitContent>(RecruitContent.class);
        List<RecruitContent> contentList = util.importExcel(file.getInputStream());
        String message = recruitContentService.importContent(contentList, recruitId, updateSupport, ShiroUtils.getLoginName());
        return AjaxResult.success(message);
    }

    @RequiresPermissions("recruit:content:import")
    @GetMapping("/importTemplate")
    @ResponseBody
    public AjaxResult importTemplate()
    {
        ExcelUtil<RecruitContent> util = new ExcelUtil<RecruitContent>(RecruitContent.class);
        return util.importTemplateExcel("招聘计划");
    }

    /**
     * 新增招聘内容
     */
    @GetMapping("/add/{recruitId}")
    public String add(@PathVariable("recruitId") Long recruitId, ModelMap mmap)
    {
        RecruitInfo recruitInfo = new RecruitInfo();
        if(recruitId != 0){
            recruitInfo = recruitInfoService.selectRecruitInfoByRecruitId(recruitId);
        }
        mmap.put("recruitInfo", recruitInfo);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/add";
    }

    /**
     * 新增保存招聘内容
     */
    @RequiresPermissions("recruit:content:add")
    @Log(title = "招聘内容", businessType = BusinessType.INSERT)
    @PostMapping("/add")
    @ResponseBody
    public AjaxResult addSave(RecruitContent recruitContent)
    {
        if (RecruitConstants.CONTENT_TYPE_NOT_UNIQUE.equals(recruitContentService.checkRecruitContentUnique(recruitContent)))
        {
            return error("新增招聘计划失败，计划已存在");
        }
        recruitContent.setCreateBy(ShiroUtils.getLoginName());
        return toAjax(recruitContentService.insertRecruitContent(recruitContent));
    }

    /**
     * 修改招聘内容
     */
    @GetMapping("/edit/{contentId}")
    public String edit(@PathVariable("contentId") Long contentId, ModelMap mmap)
    {
        RecruitContent recruitContent = recruitContentService.selectRecruitContentByContentId(contentId);
        mmap.put("recruitContent", recruitContent);
        mmap.put("infos", recruitInfoService.selectRecruitInfoList(new RecruitInfo()));
        return prefix + "/edit";
    }

    /**
     * 修改保存招聘内容
     */
    @RequiresPermissions("recruit:content:edit")
    @Log(title = "招聘内容", businessType = BusinessType.UPDATE)
    @PostMapping("/edit")
    @ResponseBody
    public AjaxResult editSave(RecruitContent recruitContent)
    {
        if (RecruitConstants.CONTENT_TYPE_NOT_UNIQUE.equals(recruitContentService.checkRecruitContentUnique(recruitContent)))
        {
            return error("修改招聘计划失败，计划已存在");
        }
        recruitContent.setUpdateBy(ShiroUtils.getLoginName());
        return toAjax(recruitContentService.updateRecruitContent(recruitContent));
    }

    /**
     * 删除招聘内容
     */
    @RequiresPermissions("recruit:content:remove")
    @Log(title = "招聘内容", businessType = BusinessType.DELETE)
    @PostMapping( "/remove")
    @ResponseBody
    public AjaxResult remove(String ids)
    {
        long count = recruitUserService.selectRecruitUserByContentId(Long.parseLong(ids)).size();
        if(count != 0){
            return error("存在报名人员，请先删除报名人员");
        }
        int written = recruitUserWrittenService.selectRecruitUserWrittenByContentId(Long.parseLong(ids));
        if(written != 0) {
            return error("存在笔试人员，请先删除笔试人员");
        }
        int interview = recruitUserInterviewService.selectRecruitUserInterviewByContentId(Long.parseLong(ids));
        if(interview != 0){
            return error("存在面试人员，请先删除面试人员");
        }
        int confirm = recruitUserConfirmService.selectRecruitUserConfirmByContentId(Long.parseLong(ids));
        if(confirm != 0){
            return error("存在到岗人员，请先删除到岗人员");
        }
        return toAjax(recruitContentService.deleteRecruitContentByContentIds(ids));
    }

    /**
     * 查询招聘内容列表
     */
    @PostMapping("/contentList")
    @ResponseBody
    public AjaxResult contentList(Long recruitId)
    {
        List<RecruitContent> contentList = recruitContentService.selectRecruitContentByRecruitId(recruitId);
        return success(contentList);
    }
}
